package com.mstore.action;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.SessionAware;

import com.mstore.pojo.Account;
import com.mstore.pojo.Microshop;
import com.mstore.service.MicroshopService;

public class MicroshopAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = -8697136830498941625L;

	private MicroshopService microshopService;

	private Microshop microshop;

	private Map<String, Object> session;

	private String result;

	/**
	 * 企业id
	 */
	private Long eaId;

	/**
	 * 保存微店地址
	 */
	public String addMicroshop() {
		return "addSuccess";
	}

	/**
	 * 删除微店地址
	 */
	public String delMicroshop() {
		if (microshop == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Long microId =  microshop.getId();
		if (microId == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		microshop = microshopService.getMicroshopUrlById(microId);
		microshopService.delMicroshop(microshop);
		return "delSuccess";
	}

	/**
	 * 修改微店地址
	 */
	public String updateMicroshop() {
		if (microshop == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		if (microshop.getId() == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		if (StringUtils.isBlank(microshop.getUrl())) {
			result = "请输入微店网址";
			return "error";
		}
		
		microshopService.updateMicroshop(microshop);
		return "updateSuccess";
	}

	/**
	 * 通过eaId查询微店地址
	 */
	public String getMicroshopUrl() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		eaId = account.getId();
		microshop = microshopService.getMicroshopUrlByEaid(eaId);

		return "getMicroshopUrl";
	}

	/**
	 * 添加微店地址
	 */
	public String addMicroshopUrl() {
		if (microshop == null) {
			return "addSuccess";
		}
		if (eaId == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		microshop.setEaId(eaId);
		microshopService.addMicroshop(microshop);

		return "addSuccess";
	}

	/**
	 * 跳转到修改页面
	 */
	public String initToModify() {
		if (microshop == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Long microId = microshop.getId();
		if (microId == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		microshop = microshopService.getMicroshopUrlById(microId);
		if (microshop == null) {
			return "returnGetMicroshopUrl";
		}
		
		return "goToModify";
	}

	public MicroshopService getMicroshopService() {
		return microshopService;
	}

	public void setMicroshopService(MicroshopService microshopService) {
		this.microshopService = microshopService;
	}

	public Microshop getMicroshop() {
		return microshop;
	}

	public void setMicroshop(Microshop microshop) {
		this.microshop = microshop;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Long getEaId() {
		return eaId;
	}

	public void setEaId(Long eaId) {
		this.eaId = eaId;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
